package cm.mefobe.android.telecomcost.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.ViewFlipper;

public class Wizard extends Dialog {

	private WizardButtons buttons;
	private ViewFlipper flipper;
	private Bundle data;
	/**
	 * @param context
	 * @param cancelable
	 * @param cancelListener
	 */
	public Wizard(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}

	/**
	 * @param context
	 * @param theme
	 */
	public Wizard(Context context, int theme) {
		super(context, theme);
	}

	public Wizard(Context context) {
		super(context);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("Wizard", "Creating the Wizard...");
		super.onCreate(savedInstanceState);

		data = new Bundle();
//		setContentView(cm.iccsoft.android.telecomcost.R.layout.wizard);
//		
//		flipper = (ViewFlipper) findViewById(R.id.flipper);
//		btnClose = (Button) findViewById(R.id.btnClose);
//		btnNext = (Button) findViewById(R.id.btnNext);
//		btnPrevious = (Button) findViewById(R.id.btnPrevious);
	}

	protected void addView(WizardView view) {
		Log.i("Wizard", "adding view to flipper...");
		view.setIndex(flipper.getChildCount());
		flipper.addView(view);
		Log.i("Wizard", "flipper child count..." + flipper.getChildCount());		
	}

	private boolean isLastView(WizardView view) {
		return view.getIndex() == flipper.getChildCount() - 1;
	}

	private boolean isFirstView(WizardView view) {
		return view.getIndex() == 0;
	}

	private void showNext() {
		WizardView view = (WizardView) flipper.getCurrentView();
		if (!isLastView(view)) {
			if (view.isValid()) {
				data.putAll(view.getData());
				view = (WizardView) flipper.getChildAt(view.getIndex() + 1);
				view.setData(data);
				setButtons();
				flipper.showNext();
			}
		} else {
			finish();
		}
	}

	private void finish() {
		hide();
	}

	private void showPrevious() {
		WizardView view = (WizardView) flipper.getCurrentView();
		if (!isFirstView(view)) {
			if (view.isValid()) {
				view = (WizardView) flipper.getChildAt(view.getIndex() - 1);
				setButtons();
				flipper.showPrevious();
			}
		} 
	}
	
	private WizardView getNextView()
	{
		WizardView currentView = (WizardView) flipper.getCurrentView();
		return (WizardView) flipper.getChildAt(currentView.getIndex() + 1);
	}
	
	private WizardView getPreviousView()
	{
		WizardView currentView = (WizardView) flipper.getCurrentView();
		return (WizardView) flipper.getChildAt(currentView.getIndex() - 1);
	}

	private void setButtons() {		
		buttons.getBtnNext().setText(getNextView().getName());
		buttons.getBtnPrevious().setText(getPreviousView().getName());
	}

	protected void closeWizard() {
		Log.i("Wizard", "closing the Wizard");
		hide();
	}

	@Override
	public void show() {
		Log.i("Wizard", "showing the Wizard...");
		super.show();
		Log.i("Wizard", "Wizard on screen...");
	}

	@Override
	protected void onStart() {
		Log.i("Wizard", "Starting the Wizard...");
		super.onStart();
		Log.i("Wizard", "Wizard started...");
	}

	@Override
	protected void onStop() {
		Log.i("Wizard", "Stopping the Wizard...");
		super.onStop();
		Log.i("Wizard", "Wizard stopped...");
	}

}
